package com.msmall.community.common.service.impl;

import java.util.Date;

import com.msmall.base.service.impl.BaseManagerImpl;
import com.msmall.base.util.ConstantDB;
import com.msmall.community.bbs.model.Topic;
import com.msmall.community.bbs.service.TopicManager;
import com.msmall.community.common.dao.AbuseDao;
import com.msmall.community.common.model.Abuse;
import com.msmall.community.common.service.AbuseManager;

public class AbuseManagerImpl extends BaseManagerImpl<Abuse, Long> implements AbuseManager{

	private AbuseDao abuseDao;
	
	private TopicManager topicManager;
	
	public void setAbuseDao(AbuseDao abuseDao) {
		this.abuseDao = abuseDao;
	}

	public void setTopicManager(TopicManager topicManager) {
		this.topicManager = topicManager;
	}

	public void audit(Abuse abuse) {
		// TODO Auto-generated method stub
		
	}

	public void audit(Long abuseId, Short audit, Long auditor, String comments) throws Exception {
		try{
			Abuse abuse = (Abuse) abuseDao.get(Abuse.class, abuseId);
			abuse.setAudit(audit);
			abuse.setAuditor(auditor);
			abuse.setAuditTime(new Date());
			if(abuse.getAbuseType().compareTo(ConstantDB.ABUSE_TYPE_TOPIC) == 0){
				Topic topic = (Topic) abuseDao.get(Topic.class, abuse.getEntity());
				if(audit.compareTo(ConstantDB.AUDIT_REJECT) == 0){
					topic.setEnabled(ConstantDB.AVALIABLE);
				}else{
					topic.setEnabled(ConstantDB.UNAVALIABLE);
				}
				
				abuseDao.saveObject(topic);
			}
			// TODO audit other entity
			abuseDao.save(abuse);
		}catch(Exception e){
			throw e;
		}
	}

	public void report(Abuse abuse) {
		if(abuse.getAbuseType().compareTo(ConstantDB.ABUSE_TYPE_TOPIC) == 0){
			reportTopic(abuse);
		}
	}

	public void report(short abuseType, Long entity, Long reporter,
			String reason, String url) {
	}
	public void reportTopic(Abuse abuse){
		
		Topic topic = (Topic) topicManager.get(Topic.class, abuse.getEntity());
		topic.setEnabled(ConstantDB.UNAVALIABLE);
		abuseDao.save(abuse);
		topicManager.save(topic);
	}

}
